import notification from "@/services/notification";
import { observable, action,makeObservable } from 'mobx';
import Employee from "@/services/employee";
import EmployeeModel from "@/store/models/EmployeeModel";
import GlobalFunctions from '@/store/globalFunctions';
import Workflow from "@/services/workflow";
import CheckDialog from "@/components/workflow/CheckDialog";
import { currentUser } from "@/services/auth";
import Enums from "@/services/enums";
import moment from "moment";

class EmployeeStore {
  @observable employeeModel = new EmployeeModel(0,0,0);
  constructor() {
    makeObservable(this)
  }
  @action initStore(id,openseapool,ifproject) {
    this.init(id,openseapool,ifproject);
  }

  init(id,openseapool,ifproject) {
    if (id !== "0" && id !== 0) {
      Employee.get({ id: id }).then(data => {
        this.employeeModel = EmployeeModel.fromJS(data);
        Workflow.edit({ bill: "employee", billtype: openseapool, billid: id, userid: currentUser.id }).then(data1 => {
          if (data1.length > 0) {
            this.employeeModel.setBilledit(data1[0]);
          }
        }).catch(error => {
        });
        
      }).catch(error => {
        this.employeeModel = new EmployeeModel(id,openseapool,ifproject);
        this.getcode("employee",moment(),0,"code");
      });
    } else {
      Employee.getid({ id: 0 }).then(data => {
        this.employeeModel = new EmployeeModel(data[0].id,openseapool,ifproject);
        this.getcode("employee",moment(),0,"code");
      });
    }
  }
  getcode(tblname,bill_date,type,tblfield) {
    Enums.getcode( {tblname: tblname,bill_date: bill_date,type: type,tblfield: tblfield,}).then(data => {
      this.employeeModel.setCode(data[0].code);
    }).catch(error => {
      notification.error(error.message, error.detailedMessage);
    });
  };

  dataSave() {
    let certificate = [];
    if (this.employeeModel.certificate1.length > 0) {
      certificate.push(this.employeeModel.certificate1.join('-'))
    }
    if (this.employeeModel.certificate2.length > 0) {
      certificate.push(this.employeeModel.certificate2.join('-'))
    }
    if (this.employeeModel.certificate3.length > 0) {
      certificate.push(this.employeeModel.certificate3.join('-'))
    }
    if (this.employeeModel.certificate4.length > 0) {
      certificate.push(this.employeeModel.certificate4.join('-'))
    }
    if (this.employeeModel.certificate5.length > 0) {
      certificate.push(this.employeeModel.certificate5.join('-'))
    }
    if (this.employeeModel.certificate6.length > 0) {
      certificate.push(this.employeeModel.certificate6.join('-'))
    }

    if (GlobalFunctions.isEmptyobj(this.employeeModel.code)) {
      notification.error("编号不能为空");
      return;
    }
    if (GlobalFunctions.isEmptyobj(this.employeeModel.name)) {
      notification.error("名称不能为空");
      return;
    }
    if (GlobalFunctions.isEmptyobj(this.employeeModel.sex)) {
      notification.error("性别不能为空");
      return;
    }
    if (GlobalFunctions.isEmptyobj(this.employeeModel.mobile)) {
      notification.error("手机号不能为空");
      return;
    }
    if (this.employeeModel.bytstatus === "签约") {
      if (GlobalFunctions.isEmptyobj(this.employeeModel.signamount) || this.employeeModel.signamount === 0) {
        notification.error("已签约的项目，金额不能为空！");
        return;
      }
      if (GlobalFunctions.isEmptyobj(this.employeeModel.signatory) || this.employeeModel.signatory === 0) {
        notification.error("已签约的项目，签约人不能为空！");
        return;
      }

      if (GlobalFunctions.isEmptyobj(this.employeeModel.contractcode)) {
        notification.error("已签约的项目，合同编号不能为空");
        return;
      }
      if (GlobalFunctions.isEmptyobj(this.employeeModel.accountname)) {
        notification.error("已签约的项目，户名不能为空");
        return;
      }
      if (GlobalFunctions.isEmptyobj(this.employeeModel.accountcode)) {
        notification.error("已签约的项目，账户不能为空");
        return;
      }
      if (GlobalFunctions.isEmptyobj(this.employeeModel.accountbank)) {
        notification.error("已签约的项目，开户行不能为空");
        return;
      }
      if (GlobalFunctions.isEmptyobj(this.employeeModel.signingtime)) {
        notification.error("已签约的项目，签约时间不能为空");
        return;
      }
      if (GlobalFunctions.isEmptyobj(this.employeeModel.expirationtime)) {
        notification.error("已签约的项目，到期时间不能为空");
        return;
      }
    }
    if (GlobalFunctions.isEmptyobj(this.employeeModel.major)) {
      notification.error("毕业专业不能为空");
      return;
    }
    if (GlobalFunctions.isEmptyobj(this.employeeModel.certificate)) {
      notification.error("证书不能为空");
      return;
    }
    let data = {
      id: this.employeeModel.id,//
      bill_date: this.employeeModel.bill_date,//--申请日期
      code: this.employeeModel.code,//----职员编码
      name: this.employeeModel.name,//--职员名称
      sex: this.employeeModel.sex,//性别
      mobile: this.employeeModel.mobile,//手机号
      idcard: this.employeeModel.idcard,//身份证号
      contacts: this.employeeModel.contacts,
      contactsmobile: this.employeeModel.contactsmobile,
      wechat: this.employeeModel.wechat,
      major: this.employeeModel.major,//毕业专业
      certificate: certificate,//证书
      location: this.employeeModel.location,//地区
      socialsecurity: this.employeeModel.socialsecurity,//社保情况（无，有）
      area: this.employeeModel.area,//社保地区
      threeperson: this.employeeModel.threeperson,//三类项目
      biddingexit: this.employeeModel.biddingexit,//招标出场（是否)
      certificatestatus: this.employeeModel.certificatestatus,
      price: this.employeeModel.price,//预计金额
      frontmoney: this.employeeModel.frontmoney,//定金
      periodunit: this.employeeModel.periodunit,//期限单位
      periods: this.employeeModel.periods,//期限
      informationfee: this.employeeModel.informationfee,//信息费
      message: this.employeeModel.message,//--纪录事项
      contracttype: this.employeeModel.contracttype,//签约类型
      bytstatus: this.employeeModel.bytstatus,
      signingtime: this.employeeModel.signingtime,//签约时间
      expirationtime: this.employeeModel.expirationtime,//到期时间
      status: this.employeeModel.status,//--状态
      openseapool: this.employeeModel.openseapool,//是否公海池 0否；1是
      maintainer: this.employeeModel.maintainer,//--维护人
      orgunitid: this.employeeModel.orgunitid,
      signamount: this.employeeModel.signamount,
      excelin: this.employeeModel.excelin,
      contractcode: this.employeeModel.contractcode,
      accountname: this.employeeModel.accountname,
      accountcode: this.employeeModel.accountcode,
      openshow: this.employeeModel.openshow,
      accountbank: this.employeeModel.accountbank,
      schooled: this.employeeModel.schooled,
      signatory: this.employeeModel.signatory,//--签约人
      userid: this.employeeModel.userid,//--操作员ID
    }
    if (this.employeeModel.isnew) {
      Employee.create(data)
        .then(() => {
          this.init(this.employeeModel.id,this.employeeModel.openseapool,0);
          notification.success("新增成功.");
        })
        .catch(error => {
          notification.error("保存失败", error.response.data.message);
        });
    } else {
      Employee.save(data)
        .then(() => {
          notification.success("修改成功.");
        })
        .catch(error => {
          notification.error("保存失败", error.response.data.message);
        });
    }
  };
  dataDelete() {
    Employee.delete({ id: this.employeeModel.id }).then(() => {
      notification.success("删除成功.");
      this.init(0);
    }).catch(error => {
      notification.error("删除失败", error.response.data.message);
    });
  };
  billCheck(userid) {
    let item = {
      bill: "employee",
      billtype: this.employeeModel.openseapool,
      billid: this.employeeModel.id,
      next_type: 1,
      message: '<div>同意</div>',
      userid: userid,
      billdate: this.employeeModel.bill_date.format('YYYY-MM-DD'),
      orgunitid: this.employeeModel.orgunitid,
      status: this.employeeModel.status,
    }
    if (this.employeeModel.status === 0) {

      Workflow.check(item).then(data => {
        if (data[0].flag === 0) {
          this.init(this.employeeModel.id,this.employeeModel.openseapool,0);
          notification.success(data[0].message);
        } else {
          notification.error(data[0].message);
        }
      })
        .catch(error => {
          notification.error(error.message, error.detailedMessage);
        });
    } else {
      CheckDialog.showModal({ bill_check: item })
        .onClose((result = {}) => {
          if (result.success) {
            this.init(this.employeeModel.id,this.employeeModel.openseapool,0);
          }
        }).onDismiss(() => {
        });
    }
  };
}

export default EmployeeStore;